package com.homework.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.homework.dto.Result;
import com.homework.entity.DailyHomework;
import com.homework.mapper.DailyHomeworkMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DailyHomeworkService {

    private final DailyHomeworkMapper dailyHomeworkMapper;

    /**
     * 获取所有日常作业
     */
    public Result<List<DailyHomework>> getAllDailyHomework() {
        List<DailyHomework> dailyHomeworks = dailyHomeworkMapper.selectList(
            new LambdaQueryWrapper<DailyHomework>()
                .orderByAsc(DailyHomework::getSequenceOrder)
        );
        return Result.success(dailyHomeworks);
    }

    /**
     * 获取启用的日常作业
     */
    public Result<List<DailyHomework>> getActiveDailyHomework() {
        List<DailyHomework> dailyHomeworks = dailyHomeworkMapper.selectList(
            new LambdaQueryWrapper<DailyHomework>()
                .eq(DailyHomework::getIsActive, true)
                .orderByAsc(DailyHomework::getSequenceOrder)
        );
        return Result.success(dailyHomeworks);
    }

    /**
     * 创建日常作业
     */
    @Transactional
    public Result<String> createDailyHomework(DailyHomework dailyHomework) {
        // 设置排序序号（如果没有设置）
        if (dailyHomework.getSequenceOrder() == null) {
            // 获取当前最大序号
            LambdaQueryWrapper<DailyHomework> wrapper = new LambdaQueryWrapper<DailyHomework>()
                .select(DailyHomework::getSequenceOrder)
                .orderByDesc(DailyHomework::getSequenceOrder)
                .last("LIMIT 1");

            DailyHomework lastHomework = dailyHomeworkMapper.selectOne(wrapper);
            int nextOrder = lastHomework != null ? lastHomework.getSequenceOrder() + 1 : 1;
            dailyHomework.setSequenceOrder(nextOrder);
        }

        dailyHomework.setIsActive(true); // 默认启用
        dailyHomeworkMapper.insert(dailyHomework);
        return Result.success("日常作业创建成功");
    }

    /**
     * 更新日常作业
     */
    @Transactional
    public Result<String> updateDailyHomework(DailyHomework dailyHomework) {
        dailyHomeworkMapper.updateById(dailyHomework);
        return Result.success("日常作业更新成功");
    }

    /**
     * 删除日常作业
     */
    @Transactional
    public Result<String> deleteDailyHomework(Long id) {
        dailyHomeworkMapper.deleteById(id);
        return Result.success("日常作业删除成功");
    }

    /**
     * 启用/禁用日常作业
     */
    @Transactional
    public Result<String> toggleDailyHomework(Long id, Boolean isActive) {
        DailyHomework dailyHomework = new DailyHomework();
        dailyHomework.setId(id);
        dailyHomework.setIsActive(isActive);
        dailyHomeworkMapper.updateById(dailyHomework);
        return Result.success(isActive ? "日常作业已启用" : "日常作业已禁用");
    }

    /**
     * 调整日常作业排序
     */
    @Transactional
    public Result<String> reorderDailyHomework(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            DailyHomework dailyHomework = new DailyHomework();
            dailyHomework.setId(ids.get(i));
            dailyHomework.setSequenceOrder(i + 1);
            dailyHomeworkMapper.updateById(dailyHomework);
        }
        return Result.success("排序调整成功");
    }
}